//
//  HeaderView.m
//  项目A --半糖
//
//  Created by romana on 16/5/16.
//  Copyright © 2016年 romana. All rights reserved.
//

#import "HeaderView.h"
#import "YYCycleScrollView.h"
#import "AFNetworking.h"
#import "HeaderViewModel.h"
#import "UIImageView+WebCache.h"

@implementation HeaderView

-(instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    if (self) {
      
        [self doIt];
    }

    return self;
}

-(void)doIt{

    self.ImageArray = [NSMutableArray array];
    self.backgroundColor = [UIColor whiteColor];
    YYCycleScrollView *cycleScrollView = [[YYCycleScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) animationDuration:2.0];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:@"http://open3.bantangapp.com/recommend/index?app_id=com.jzyd.BanTang&app_installtime=1434705307&app_versions=5.7&channel_name=appStore&client_id=bt_app_ios&client_secret=9c1e6634ce1c5098e056628cd66a17a5&last_get_time=1463491553&os_versions=9.3.1&page=0&pagesize=20&screensize=1242&track_device_info=iPhone7%2C1&track_deviceid=C2D3B460-4E0E-4639-A030-D274CFC43D50&v=12%20HTTP/1.1" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSMutableDictionary *bigDic = responseObject;
        NSMutableDictionary *dataDic = [bigDic objectForKey:@"data"];
        NSMutableArray *bannerArray = [dataDic objectForKey:@"banner"];
        for (NSDictionary *dic in bannerArray) {
                    HeaderViewModel *HVModel = [[HeaderViewModel alloc] init];
                    [HVModel setValuesForKeysWithDictionary:dic];
            [self.ImageArray addObject:HVModel.photo];
            
        }
        NSMutableArray *viewArray = [[NSMutableArray alloc] init];
        for (int i = 0; i < self.ImageArray.count; i++){
        
            UIImageView *tempImageView = [[UIImageView alloc] initWithFrame:self.frame];
            tempImageView.backgroundColor = [UIColor whiteColor];
            [tempImageView sd_setImageWithURL:[NSURL URLWithString:self.ImageArray[i]]];
            tempImageView.contentMode = UIViewContentModeScaleAspectFill;
            tempImageView.clipsToBounds = true;
            [viewArray addObject:tempImageView];

        
        }
        [cycleScrollView setFetchContentViewAtIndex:^UIView *(NSInteger(pageIndex)) {
            return [viewArray objectAtIndex:pageIndex];
        }];
        [cycleScrollView setTotalPagesCount:^NSInteger{
            return self.ImageArray.count;
        }];
        [cycleScrollView setTapActionBlock:^(NSInteger(pageIndex)) {
            
            NSLog(@"点击的相关的页面%ld",(long)pageIndex);
            
        }];
        
        [self addSubview:cycleScrollView];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];


}




@end
